package com.codebattery.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/** 
 * @ClassName: CatchIMG 
 * @Description: 爬取一个指定地址的网络图片
 * @author penny
 * @date 2017年12月3日 下午9:00:05 
 * 
 */
public class CatchImgUtil {

	/**
	 * 
	 * @Title: getImg 
	 * @Description: 通过一个url 去获取图片
	 * @param @param url 图片的连接地址
	 * @param @throws IOException    
	 * @throws
	 */
	public static InputStream getImgStream(String url) throws IOException{
		InputStream ins = null;
		URL imgURL = new URL(url.trim());//转换URL
		HttpURLConnection urlConn = (HttpURLConnection) imgURL.openConnection();//构造连接
		urlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36");
		urlConn.connect();
		System.out.println(CatchImgUtil.class.toString()+":获取连接="+urlConn.getResponseMessage());
		if(urlConn.getResponseCode()==200){//返回的状态码是200 表示成功
			ins = urlConn.getInputStream(); //获取输入流,从网站读取数据到 内存中
		}
		return ins;
	}

}
